package org.iabako.client.ui.form.extranet.demo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.init.Iabako;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.extranet.EmailVerificationForm;
import org.iabako.client.ui.layout.field.InputTextEmail;
import org.iabako.client.ui.layout.field.InputTextPassword;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.security.AuthenticateServiceAsync;

/**
 * Created by jose on 06/01/15.
 */
public abstract class GoToDemoForm extends EmailVerificationForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, GoToDemoForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    private AuthenticateServiceAsync authenticateServiceAsync = AuthenticateServiceAsync.Util.getInstance();

    private int attempts;
    @UiField
    InputTextEmail emailForLogin;
    @UiField
    InputTextPassword passwordForLogin;
    @UiField
    FormPanel signInForm;

    public GoToDemoForm() {
        simplePopup = true;
        setForm(binderForm, PageConfigEnum.SignUp, null);

        goAheadEmailHandler();
        goAheadVerificationHandler();

        //Spring security
        emailForLogin.getInputField().setName("j_username");
        passwordForLogin.getInputField().setName("j_password");
        //We don't want user be asked for password reminder
        passwordForLogin.getInputField().getElement().setAttribute("type", "password");

        signInForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {
                authenticateServiceAsync.getAuthenticatedUser(new IabakoAsyncCallback<User>() {
                    public void onSuccess(User user) {
                        if (user != null) {
                            //Sign in success
                            Iabako.showHomePage();
                        } else {
                            NavigationController.getInstance().closePopups();
                            NavigationController.getInstance().show(new IabakoDialogBox("go_to_demo_error_title", "go_to_demo_error_text"));
                        }
                        super.onSuccess(user);
                    }
                });
            }
        });
    }

    @Override
    protected void customSendKeyCodeAction(User result) {
        emailPanel.setVisible(false);

        initializeEmailVerificationPanel("go_to_demo_email_verification_text", "password_reminder_email_verification_help");
        ValidationMessage.getFirstFromDOM().clear();
        emailVerificationPanel.setVisible(true);
    }

    @Override
    protected void customVerifyKeyCodeAction(Boolean result) {
        attempts++;

        if (result != null && result) {

            if (Language.ES.getGwtCode().equalsIgnoreCase(LocaleInfo.getCurrentLocale().getLocaleName())) {
                emailForLogin.setValue("demo.es@iabako.com");
            } else if (Language.FR.getGwtCode().equalsIgnoreCase(LocaleInfo.getCurrentLocale().getLocaleName())) {
                emailForLogin.setValue("demo.fr@iabako.com");
            } else {
                emailForLogin.setValue("demo.en@iabako.com");
            }

            passwordForLogin.setValue("password");

            signInForm.submit();

        } else if (attempts < 3) {

            keyCode.addErrorMessage(keyCode, false, "validation_key_code_error");

        } else {
            NavigationController.getInstance().closePopups();
            NavigationController.getInstance().show(new IabakoDialogBox("sign_up_attempts_limit_error_title", "sign_up_attempts_limit_error_message"));
        }
    }
}
